package com.njtoyo.taxi.admin.logic.finance;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverWithdraw;
import com.njtoyo.taxi.admin.mapper.master.enums.DriverWithdrawState;
import com.njtoyo.taxi.admin.mapper.master.mapper.BankWithdrawActionMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverWithdrawMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

@Component
@RequiredArgsConstructor
public class DriverWithdrawLogic {
    private final DriverWithdrawMapper driverWithdrawMapper;
    private final BankWithdrawActionMapper bankWithdrawActionMapper;

    @Transactional
    public void pay(Long withdrawId) {
        DriverWithdraw withdraw = driverWithdrawMapper.selectById(withdrawId);
        if (Objects.isNull(withdraw)) {
            throw new RuntimeException("提现申请不存在");
        }
        if (!withdraw.getState().equals(DriverWithdrawState.init) && !withdraw.getState().equals(DriverWithdrawState.failed)) {
            throw new RuntimeException("非法操作，此提现申请当前不可打款");
        }
        new LambdaUpdateChainWrapper<>(driverWithdrawMapper)
                .eq(DriverWithdraw::getId, withdrawId)
                .set(DriverWithdraw::getState, DriverWithdrawState.done)
                .set(DriverWithdraw::getUpdatedAt, new Date())
                .update();
        // 查询打款记录是否可打款
        // 记录打款
        // 修改状态
    }
}
